package com.ygg.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.ygg.model.hosp.Department;
import com.ygg.repository.DepartmentRepository;
import com.ygg.service.DepartmentService;
import com.ygg.vo.hosp.DepartmentQueryVo;
import com.ygg.vo.hosp.DepartmentVo;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {

        Department department = BeanUtil.copyProperties(map, Department.class);

        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (targetDepartment == null) {
            //新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            //修改
            department.setId(targetDepartment.getId());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findPageDepartment(Integer pageNum, Integer pageSize, DepartmentQueryVo vo) {
        //创建排序规则
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //创建分页构造器
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        //封装实体
        Department department = BeanUtil.copyProperties(vo, Department.class);
        department.setIsDeleted(0);

        //创建匹配器,即如何使用查询条件  (模糊查询且忽略大小写)
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //创建实例
        Example<Department> example = Example.of(department, exampleMatcher);

        return departmentRepository.findAll(example, pageable);
    }

    @Override
    public void removeDepartment(Map<String, Object> map) {
        String hoscode = map.get("hoscode").toString();
        String depcode = map.get("depcode").toString();

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> of = Example.of(department);
        //所有科室列表 departmentList
        List<Department> departments = departmentRepository.findAll(of);

        //根据大科室编号分类
        Map<String, List<Department>> collect = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map集合 deparmentMap
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            //封装大课室
            DepartmentVo bigDepartment = new DepartmentVo();
            bigDepartment.setDepcode(bigcode);
            bigDepartment.setDepname(deparment1List.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> list = deparment1List.stream().map(k -> BeanUtil.copyProperties(k, DepartmentVo.class)).collect(Collectors.toList());

            bigDepartment.setChildren(list);
            //加入集合中
            result.add(bigDepartment);
        }

        return result;

    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) return department.getDepname();
        return "";
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

    }
}
